package com.hl.toolkit.admin.controller;

import com.tycoin.common.bean.BaseResult;
import com.tycoin.common.bean.PageInfo;
import com.tycoin.common.controller.AbstractController;
import com.tycoin.common.enums.ValidStatusEnum;
import com.tycoin.common.utils.BeanInfoUtil;
import com.tycoin.user.slaveentity.TycoinUser;
import com.tycoin.user.slaverepository.TycoinUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = {"/tycoinuser/tycoinuser"})
public class TycoinUserController extends AbstractController {

    @Autowired
    private TycoinUserRepository appRepository;

    @RequestMapping(value = {""})
    public String list(Model model, HttpServletRequest request, PageInfo pageInfo) {
        Page<TycoinUser> page = find(request, pageInfo, appRepository);
        model.addAttribute("page", page);

        model.addAttribute("statusEnum", ValidStatusEnum.values());

        return "tycoinuser/tycoinuser/list";
    }

    @RequestMapping(value = {"add"}, method = RequestMethod.GET)
    public String toAdd(Model model) {
        model.addAttribute("statusEnum", ValidStatusEnum.values());

        return "tycoinuser/tycoinuser/edit";
    }

    @RequestMapping(value = {"add"}, method = RequestMethod.POST, consumes = "application/json; charset=UTF-8", produces = "application/json; charset=UTF-8")
    @ResponseBody
    public BaseResult add(@RequestBody TycoinUser app) {
        return edit(app, null);
    }

    @RequestMapping(value = {"{id}/edit"}, method = RequestMethod.GET)
    public String toEdit(Model model, @PathVariable("id") Long id) {
        TycoinUser tycoinUser = appRepository.findOne(id);
        model.addAttribute("tycoinUser", tycoinUser);

        model.addAttribute("statusEnum", ValidStatusEnum.values());
        return "tycoinuser/tycoinuser/edit";
    }

    @RequestMapping(value = {"{id}/edit"}, method = RequestMethod.POST, consumes = "application/json; charset=UTF-8", produces = "application/json; charset=UTF-8")
    @ResponseBody
    public BaseResult edit(@RequestBody TycoinUser app, @PathVariable("id") Long id) {
        if (id == null) {
            appRepository.save(app);
        } else {
           TycoinUser appInfo = appRepository.findOne(app.getId());
            BeanInfoUtil.bean2bean(app, appInfo, "name,code,status,description");
            appRepository.save(appInfo);
        }
        return new BaseResult();
    }

    @RequestMapping(value = {"{id}/delete"}, produces = "application/json; charset=UTF-8")
    @ResponseBody
    public BaseResult delete(@PathVariable("id") Long id) {
        appRepository.delete(id);
        return new BaseResult();
    }

    /**@RequestMapping(value = {"check"}, produces = "application/json; charset=UTF-8")
    @ResponseBody
    public Object[] check(@RequestParam(value = "id", required = false) Long id, @RequestParam(value = "fieldId") String fieldId,
            @RequestParam(value = "fieldValue") String fieldValue) {
        boolean exist = checkCode(id, fieldValue);

        Object[] jsonValidateReturn = new Object[3];
        jsonValidateReturn[0] = fieldId;
        jsonValidateReturn[1] = !exist;
        if (!exist) {
            jsonValidateReturn[2] = "可以使用!";
        } else {
            jsonValidateReturn[2] = "代码" + StringUtils.trim(fieldValue) + "已使用!";
        }
        return jsonValidateReturn;
    }

    private boolean checkCode(Long id, String code) {
        boolean exist = false;
        tycoinUser app = null;
        if (StringUtils.isNotEmpty(code)) {
            app = appRepository.findOneByCode(code);
        }
        if (app != null) {
            if (id == null || id == -1 || app.getId().longValue() != id) {
                exist = true;
            }
        }
        return exist;
    }**/

}
